﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
using BookShop;
using SubSonic;

public partial class UserControl_TransArrange : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                ObjectDataSource detail = e.Item.FindControl("ObjectDataSource2") as ObjectDataSource;
                Label label = (Label)e.Item.FindControl("StoreIDLabel");
                string storeId = label.Text;
                detail.SelectParameters["AllocationID"].DefaultValue = DataList1.DataKeys[e.Item.ItemIndex].ToString();
                detail.SelectParameters["StoreID"].DefaultValue = storeId;

            }
            if (e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ObjectDataSource detail = e.Item.FindControl("ObjectDataSource2") as ObjectDataSource;
                Label label = (Label)e.Item.FindControl("StoreIDLabel");
                string storeId = label.Text;
                detail.SelectParameters["AllocationID"].DefaultValue = DataList1.DataKeys[e.Item.ItemIndex].ToString();
                detail.SelectParameters["StoreID"].DefaultValue = storeId;
            }
        }


    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex; 
        
        //ObjectDataSource1.DataBind();
        DataList1.DataBind();
       
        
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        string allocationId, storeId, shipVia;
        DateTime shipDate;
        allocationId = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        Label label = (Label)e.Item.FindControl("StoreIDLabel");
        storeId = label.Text;
        DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
        shipVia = ddl.SelectedValue;
        TextBox textbox = (TextBox)e.Item.FindControl("TextBox1");
        shipDate = Convert.ToDateTime(textbox.Text);
        new AllocationController().Update(allocationId, shipDate, shipVia, storeId);
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        DataSet ds = new Select("AllocationID").From("Allocation").Where("Status").IsEqualTo("ready").ExecuteDataSet();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            new FlowProcess().FlowExecute("2", ds.Tables[0].Rows[i][0].ToString(), Profile.UserName);
            //Response.Write("<script>window.open('TransArrage.aspx?source=" + ds.Tables[0].Rows[i][0].ToString() + "')<script/>");
        }

    }
}
